<!DOCTYPE HTML>
<?php
session_start();
date_default_timezone_set('Europe/Paris');
?>

<html>

    <head>
        <title>BLOG</title>
        <meta charset="utf8">
        <?php include 'Include.php';?>

        <style type="text/css">

            .content{
                margin:2% 10% 0 10%;
                color:white;
            }
			
            .background-img{
                position:abs;
                z-index: -1;
                overflow:visible;
                height:auto;
                min-height:100vh;

            }
			
            body{
                min-width:800px;
                width:auto!important;
                width:800px;
				height:2000px;
                background-image:url(../img/sharp_edge.png);
                background-repeat:repeat;
				<!--background-attachment:fixed;-->
            }
			
			input{
				color:black;
			}
			
			td{
				vertical-align:0;
				padding-bottom:10px;
				word-wrap:break-word;
			}
			
			.contentleft{
				float:left;
			}
			
			.clearfloat{
				clear:both;
			}
			
			.contentright{
				padding-left:520px;
			}

			.collapse.in{
				margin-right:20px;
				width:500px;
			}
			
			.collapsing{
				width:500px;
				margin-right:20px;
			}
			
        </style>

    </head>

    <script type="text/javascript">

        function modifarticle(art_id) {
            document.location.href = "modifier_art.php?art_id=" + art_id;
        }

        function delarticle(art_id) {
            document.location.href = "../controleur/delete_art.php?art_id=" + art_id;
        }

        function hide_conf(conf_id) {

            if (document.getElementById(conf_id).style.visibility == "hidden")
                document.getElementById(conf_id).style.visibility = "visible";
            else
                document.getElementById(conf_id).style.visibility = "hidden";
            return true;

        }
		
		function delete_comm(comm_id){
			document.location.href = "../controleur/delete_comm.php?comm_id=" + comm_id;
		}

    </script>

<script type="text/javascript" src="../controleur/compteur.php"></script>

    <body>
        <?php include 'navBar.php'; ?>
            <?php
            // include '../model/model_sql.php';
            $myvar = new SQLBlog();
            if ($var = $myvar->AfficherPost()) {
                foreach ($var as $line) {
                    if ($line['actif'] != 0){
                        echo "	<div class=\"content ".$line['id_utilisateur']." cat".$line['id_categorie']."\">";
							echo "		<h3 style=\"margin-top:0;display:inline;\">" . $line['titre'] . "</h3>";
							if (isset($_SESSION['login']) && isset($_SESSION['id_droit']) && ($_SESSION['id_droit'] == 1 || $_SESSION['id'] == $line['id_utilisateur'])) {
								echo "<button style=\"margin:0 0 10px 10px;\" type=\"button\" name=\"modif\" class=\"btn btn-sm btn-success\" onclick=\"modifarticle(" . $line['id'] . ")\"><span class=\"glyphicon glyphicon-wrench\"></span></button>";
								echo "<button style=\"margin:0 0 10px 10px;\" type=\"button\" name=\"delete\" class=\"btn btn-sm btn-danger\" onclick=\"hide_conf('conf_" . $line['id'] . "')\"><span class=\"glyphicon glyphicon-trash\"></span></button>";
								echo "<button style=\"margin:0 0 10px 10px;visibility:hidden;\" type=\"button\" name=\"confirm\" class=\"btn btn-sm btn-warning\" id=\"conf_" . $line['id'] . "\" onclick=\"delarticle(" . $line['id'] . ")\">Confirmer</button>";
							}
							else echo "<br/>";
							echo "<div class=\"clearfloat\"></div>";
							echo "<div class=\"contentleft\">"; if($line['piecesJointes']){echo "<img src=\"".$line['piecesJointes']."\" style=\"width:500px;margin:15px 20px 10px 0;\"/>";}
							if ($line['commentaireAutorise']==1){
								echo "<br/><button class=\"btn btn-primary\" type=\"button\" data-toggle=\"collapse\" data-target=\"#collapseExample_".$line['id']."\" aria-expanded=\"false\" aria-controls=\"collapseExample_".$line['id']."\">Commentaires</button>";
								echo "<div class=\"collapse\" style=\"margin-top:7px;\" id=\"collapseExample_".$line['id']."\">";
								if (isset($_SESSION['login'])) {
									echo "<form method=\"POST\" action=\"../controleur/add_comment.php\"><input type=\"hidden\" name=\"art_id\" value=\"".$line['id']."\"/><input id=\"text_comment\" type=\"text\" name=\"comment\" style=\"float:left;height:30px;width:80%;max-width:85%;border:none;\" required/><input type=\"submit\" value=\"Commenter\" style=\"height:30px;width:20%;border:none;background-color:green;\"/></form>";
								}	
								echo "<div style=\"background:white;color:black;height:300px;overflow-y:scroll;margin-top:5px;padding:20px;\">";
								if($var_com = $myvar->AfficherCommentairesParPost($line['id'])){
									echo "<table style=\"table-layout: fixed;width:100%;\">";
									
									foreach ($var_com as $line_com){
										echo "<tr>";
										if (isset($_SESSION['login']) && isset($_SESSION['id_droit']) && $_SESSION['id_droit'] == 1) {
											echo "<td style=\"width:10%;\"><button class=\"btn btn-xs btn-danger\" title=\"Supprimer le commentaire\" onclick=\"delete_comm(".$line_com['id'].")\"><span class=\"glyphicon glyphicon-remove-sign\"></span></button></td>";
										}
										echo "<td style=\"width:25%;\"><b><span style=\"font-size:18px;\">".$line_com['prenom']." ".$line_com['nom']."</span></b><br/>Le ".date("d-m-Y", strtotime($line_com['dateCreation']))."<br/>A ".date("H:i:s", strtotime($line_com['dateCreation']))."</td><td style=\"padding-left:40px;width:65%;\">".$line_com['texte']."</td></tr>";
									}
									echo "</table>";
								}
								echo "	</div>";
								echo "</div>";
							}
							else{
								echo "<br/><span style=\"color:red;\">Les commentaires sont desactives pour cet article</span>";
							}
							echo "</div>";
							echo "<div class=\"contentright\">"; echo "<p style=\"text-align:justify;\">".nl2br( $line['texte'])."</p>"; echo "</div>";
							echo "<div class=\"clearfloat\"></div>";
							
							
							
							
							
							
							
						echo "</div>";
                    }
					else{
						if (isset($_SESSION['login']) && isset($_SESSION['id_droit']) && ($_SESSION['id_droit'] == 1 || $_SESSION['id'] == $line['id_utilisateur'])) {
							echo "	<div class=\"content ".$line['id_utilisateur']." cat".$line['id_categorie']."\">";
							echo "		<h3 style=\"margin-top:0;display:inline;color:grey;\">" . $line['titre'] . "</h3> ";
							echo "<button style=\"margin:0 0 10px 10px;\" type=\"button\" name=\"modif\" class=\"btn btn-sm btn-default\" onclick=\"modifarticle(" . $line['id'] . ")\"><span class=\"glyphicon glyphicon-wrench\"></span></button>";
                            echo "<button style=\"margin:0 0 10px 10px;\" type=\"button\" name=\"delete\" class=\"btn btn-sm btn-default\" onclick=\"hide_conf('conf_" . $line['id'] . "')\"><span class=\"glyphicon glyphicon-trash\"></span></button>";
                            echo "<button style=\"margin:0 0 10px 10px;visibility:hidden;\" type=\"button\" name=\"confirm\" class=\"btn btn-sm btn-default\" id=\"conf_" . $line['id'] . "\" onclick=\"delarticle(" . $line['id'] . ")\">Confirmer</button>";
							echo "<div class=\"clearfloat\"></div>";
							echo "<div class=\"contentleft\">";
							if($line['piecesJointes']){echo "<img src=\"".$line['piecesJointes']."\" align=\"left\" style=\"width:500px;margin:15px 20px 10px 0;filter: grayscale(100%);-webkit-filter: grayscale(100%);-moz-filter: grayscale(100%);-ms-filter: grayscale(100%);-o-filter: grayscale(100%);\"/>";}
							echo "</div>";
							echo "<div class=\"contentright\">";
							echo "<p style=\"text-align:justify;color:grey;\">".nl2br( $line['texte'])."</p>";
							echo "</div>";
							echo "</div>";
						}
					}
                }
				echo "<div style=\"height:200px;\"></div>";
            }
            ?>
			
			 
    </body>   
	
	<?php include('footer.php'); ?>

</html>